package done.myra.excercise.ch9;

public class MailTest {

	//用户名
	private String username;
	//域名
	private String domainName;
	//提示信息
	private String message;
	
	public static void main(String[] args) {

		//定义对象
		MailTest mT = new MailTest();
		//定义几个email
		String[] email = {"leii.23@we.122", "leii23@we.12@2","leoo@89.com","oip,com@09.cn","yuoo.com"};
		//for循环检查是否合法
		for(int i=0;i<email.length;i++) {
			//输出测试信息
			System.out.println("testing..."+email[i]+" is Legal:");
			//如果邮箱名正确 输出用户名和域名
			if(mT.testMail(email[i])) {
				System.out.println("输入的邮箱是"+email[i]+"用户名是"+mT.username+"域名是"+mT.domainName);
			}
			System.out.println();
		}
		
		
//		String email1= "leoo@89.com";
//		boolean b1 = mT.testMail(email1);
		
		
		
		
		
	}
	
	//方法
	private   boolean testMail(String email) {
		//判断是否合法
		boolean isLegal=false;
		//字符数组接收字符串
		char[] charArr = email.toCharArray();
		
		//统计@的个数
		int count1=0;
		//标记@的索引
		int index1=0;
		//统计.的个数
		int count2 = 0;
		//标记.的索引
		int index2 = 0;
		//是否包含其他字符
		boolean isOthers = false;
		//计算其他字符个数
		int count3 =0;
		//遍历输出数组
//		for (char c : charArr) {
//			System.out.print( c+ " ");
//		}
		
		
		
		//遍历查找
		for(int i =0;i<charArr.length;i++) {
			if(charArr[i]=='@') {
				count1++;
				index1 = i;
			}else if(charArr[i] == '.') {
				count2++;
				index2 = i;
			}else if((charArr[i]>='0'&&charArr[i]<='9')
					||(charArr[i]>='a'&&charArr[i]<='z')
					||(charArr[i]>='A'&&charArr[i]<='Z')) {
				;
			}else {
				count3 ++;
				isOthers = true;
//				System.out.println(charArr[i]);
			}
			
		}
		
//		System.out.println();
		//以下为代码错误分析使用到的显示输出
		//错误原因：判断字符是不是在0-9之间是没加单引号！！！
		//遍历完成后 个个值为
//		System.out.println("count1="+count1+" count2="+count2+" index1="+index1+" index2="+index2+" count3="+count3);
//		
//		if(isOthers==false&&count1==1&&count2==1&&index1<index2&&count3==0) {
//			this.username = "hello";
//			this.domainName = "World";
//			isLegal = true;
//		}
//		
		if(isOthers) {
			System.out.println("邮箱输入有误：用户名里有其他字符");
		}else if(count1>1||count2>1) {
			System.out.println("邮箱输入有误：邮箱地址中含有多了@或.");
		}else if(index1>=index2) {
			System.out.println("邮箱输入有误：邮箱地址中.出现在@的前面");
		}else if(index1 == 0 || index2 == 0) {
			System.out.println("邮箱输入有误：邮箱地址中没有@或者.");
		}else if(count1 == 1&& count2 ==1 && index1<index2) {

			isLegal = true;
			username = email.substring(0, index1);
			domainName = email.substring(index1+1);
		}
//		
	
		//返回值
		return isLegal;
	}

	
	
	
	
	/**
	 * 
	 */
	public MailTest() {
		super();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getDomainName() {
		return domainName;
	}

	public void setDomainName(String domainName) {
		this.domainName = domainName;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	

}
